local Msg = require("core.Msg")
local Map = require("core.Map")
local ExcelMapConf = require("excel.Map")
local ExcelNpcConf = require("excel.Npc")
local hasInitJumpInfos

local function initJumpInfos()
	if hasInitJumpInfos then
		return
	end
	hasInitJumpInfos = true
    local gcJumpInfo = Msg.gc.GC_JUMP_INFO
    local count = 0
    for k in pairs(ExcelMapConf.scene) do
		for index, sceneEvent in pairs(ExcelMapConf[k]) do
			if sceneEvent.action_cmd1 == "jump" then
				-- print("sceneEvent mapID:", k, " has jump")
				local listRect = Map.getRects(k, sceneEvent.action_arg1[1])
				local rect = listRect[1]
				if not rect then
					print(k, sceneEvent.action_arg1[1])
					assert()
				end
				count = count + 1
				gcJumpInfo.jumps[count].npcId = 0
				gcJumpInfo.jumps[count].x = (rect[1] + rect[3]) / 2
				gcJumpInfo.jumps[count].y = (rect[2] + rect[4]) / 2
				gcJumpInfo.jumps[count].srcMapId = k
				gcJumpInfo.jumps[count].desMapId = sceneEvent.action_arg1[2]
			end
		end
    end
    
    for npcID, conf in pairs(ExcelNpcConf.npc) do
		local ability = conf.ability
		for key, value in pairs(ability) do
			if value >= 100 then
			    local copyid = math.floor(value / 100)%1000
				if ExcelMapConf.copy[copyid] then
					count = count + 1
					gcJumpInfo.jumps[count].npcId = npcID
					gcJumpInfo.jumps[count].x = conf.mapX
					gcJumpInfo.jumps[count].y = conf.mapY
					gcJumpInfo.jumps[count].srcMapId = conf.mapID
					gcJumpInfo.jumps[count].desMapId = ExcelMapConf.copy[copyid].mapList[1]
					
					--print("desmapId:", ExcelMapConf.copy[copyid].mapList[1])
				else
					print("npc not in world! ", npcID)
				end
			end
		end
    end
    gcJumpInfo.jumps[0] = count

    local count2 = 0
    for mapId, conf in pairs(ExcelMapConf.scene) do 
        count2 = count2 + 1
        gcJumpInfo.maps[count2].mapId = mapId
        gcJumpInfo.maps[count2].name = conf.name
        gcJumpInfo.maps[count2].isPVP = conf.isPVP > 0 and 1 or 0
        gcJumpInfo.maps[count2].isMining = conf.isMining
        gcJumpInfo.maps[count2].noHang = conf.noHang
        gcJumpInfo.maps[count2].fog = conf.fog
        gcJumpInfo.maps[count2].isDoubleLayer = conf.isDoubleLayer
        gcJumpInfo.maps[count2].canAvoidWar = conf.canAvoidwar
        gcJumpInfo.maps[count2].showEnemy = conf.show_enemy
        gcJumpInfo.maps[count2].mp3 = conf.mp3        
    end
    gcJumpInfo.maps[0] = count2
end

function sendJumpInfos(human)
	initJumpInfos()
	Msg.send(Msg.gc.GC_JUMP_INFO, human.fd)
	
	--print("SendJumpInfos mapLen:", Msg.gc.GC_JUMP_INFO.maps[0], " jumpLen:", Msg.gc.GC_JUMP_INFO.jumps[0])
end